package com.hcms.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hcms.common.Result;
import com.hcms.entity.Association;
import com.hcms.entity.Essay;
import com.hcms.mapper.AssociationMapper;
import com.hcms.mapper.EssayMapper;
import com.hcms.mapper.UserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@RequestMapping("/essay")
public class EssayController {

    @Resource
    EssayMapper essayMapper;

    @PostMapping
    public Result<?> save(@RequestBody Essay essay){
        essay.setTime(new Date());
        essayMapper.insert(essay);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody Essay essay){
        essayMapper.updateById(essay);
        return Result.success();
    }

    @DeleteMapping("/{id}") //删除
    public Result<?> update(@PathVariable Long id){
        Integer a = essayMapper.deleteById(id);
        System.out.println(a);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id) {
      return Result.success(essayMapper.selectById(id));
    }


    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search){
        LambdaQueryWrapper<Essay> wrapper = Wrappers.<Essay>lambdaQuery();
        if (StrUtil.isNotBlank(search))
        {
             wrapper.like(Essay::getTitle, search);
        }
        Page<Essay> essayPage = essayMapper.selectPage(new Page<>(pageNum,pageSize),wrapper);
        return Result.success(essayPage);
    }

}
